package scenic;

import java.awt.Rectangle;

import scenic.geom.Affine2;
import scenic.jni.Renderer;

/**
 * This is the base class for shapes. Shapes are scene nodes that 
 * specify an area. The area of the shape is drawn with a color. 
 * The default color is white (red=1, green=1, blue=1, alpha=1).
 */
public abstract class Shape extends SceneLeaf {
	private static final Color white = new Color(1.0f, 1.0f, 1.0f, 1.0f);
	
	private Color color = white;
	
	public Shape() {
		super();
	}

	/**
	 * Gets the shape color.
	 */
	public Color getColor() {
		return color;
	}
	
	/**
	 * Sets the shape color.
	 */
	public void setColor(Color color) {
		this.color = color;
		changed();
	}
	
	void draw(DrawContext context, Affine2 transform, Rectangle visibleArea) {
		int surfaceType = getSurfaceType(getDrawType(transform, color));
		
		if(surfaceType == Renderer.SURFACE_TYPE_COLOR || 
				(surfaceType == context.surfaceType && color.isWhite())) {
			draw(context, transform, color);
		} else {
			Rectangle bounds = getBounds(transform);
			
			if(bounds.width <= 0 || bounds.height <= 0)
				return;
			if(context.renderer.beginSurface(context.context, addSafeMargin(bounds), surfaceType) == 0)
				return;
			draw(context, transform, white);
			context.renderer.color(context.context, color);
			context.renderer.drawSurface(context.context);
		}
	}
	
	abstract void draw(DrawContext context, Affine2 transform, Color color);

	void prepareDraw(DrawContext context, Affine2 transform) {
	}
	
	int getDrawType(Affine2 transform) {
		return getDrawType(transform, color);
	}
	
	int getDrawType(Affine2 transform, Color color) {
		return DRAW_SIMPLE;
	}
	
	abstract Rectangle getBounds(Affine2 transform);
}
